// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.

using System;
using System.IO;
using SrtSync.Core.Framework;
using SrtSync.Core.Notifications;

namespace SrtSync.Core.Data
{
    public class Application : Observable<Notification<ApplicationModelNotificationTypes>>, IApplication
    {
        private Editor _leftEditor;
        private Editor _rightEditor;
        private Settings _settings;
        private bool _firstRun;

        public ISettings Settings
        {
            get { return _settings; }
        }

        public void Synchronize(string path)
        {
            SrtWriter writer = new SrtWriter();
            writer.Synchronize(path, _leftEditor.Encoding, _leftEditor.Entries, _rightEditor.Entries);
        }

        public void Terminate()
        {
            Notify(new Notification<ApplicationModelNotificationTypes> { Type = ApplicationModelNotificationTypes.Terminate });

            _settings.Save();
        }

        public Application()
        {
            InitiliazeSettings();
            InitializeEditors();
        }

        public IEditor Left
        {
            get
            {
                return _leftEditor;
            }
        }

        public IEditor Right
        {
            get
            {
                return _rightEditor;
            }
        }

        public bool FirstRun
        {
            get { return _firstRun; }
        }

        private void InitiliazeSettings()
        {
            string userProfileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            if (!String.IsNullOrEmpty(userProfileDirectory) && Directory.Exists(userProfileDirectory))
            {
                string path;
                switch (Environment.OSVersion.Platform)
                {
                    case PlatformID.Unix:
                    case PlatformID.MacOSX:
                        path = Path.Combine(userProfileDirectory, ".srtsync.xml");
                        break;
                    default:
                        path = Path.Combine(userProfileDirectory, Path.Combine("SrtSync", "Settings.xml"));
                        break;
                }
                _settings = Core.Settings.Load(path, out _firstRun);
            }
        }

        private void InitializeEditors()
        {
            _leftEditor = new Editor { Encoding = _settings.LeftEncoding.Value };
            _rightEditor = new Editor { Encoding = _settings.RightEncoding.Value };
        }

        public void Dispatch(ApplicationModelNotificationTypes data)
        {
            Notify(new Notification<ApplicationModelNotificationTypes> { Type = ApplicationModelNotificationTypes.Start });
        }
    }
}